package com.ak.demo.controller;

import com.ak.demo.config.LoginJob;
import com.ak.demo.config.UrlComponent;
import com.ak.demo.config.VisitJob;
import com.ak.demo.dao.UserMapper;
import com.ak.demo.entity.User;
import com.ak.demo.service.impl.LoginServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**“
 * 用来处理健康打卡模块的
 */
@Controller
@Slf4j
public class LoginController {

    @Autowired
    LoginServiceImpl loginService;

    @Autowired
    UserMapper userMapper;

    @Qualifier("MyScheduler")
    @Autowired
    Scheduler scheduler;


    /**
     * 返回 添加 打卡人员的链接
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "add";
    }


    /**
     * 添加用户名 和密码
     */
    @RequestMapping("/addUser")
    public void add(String username,String password,String email){
        loginService.login(username,password,email);
    }




    /**
     * 添加
     * @param model
     * @return
     */
    @RequestMapping("/count")
    public String count(Model model){

        List<String> ss=new ArrayList<>();
        List<User> users=userMapper.selectList(null);
        for(User user:users){
            ss.add(user.getUsername());
        }
        model.addAttribute("user",ss);
        return "count";
    }


    /**
     * 开始两个进程，一个是定时访问
     */
    @RequestMapping("/start")
    @ResponseBody
    public String start(){
        try {
            scheduler.start();
            UrlComponent.start=true;
            UrlComponent.date = new Date();
            return "项目启动成功";
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "项目启动失败";

    }
}
